package com.news.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.User;
import com.news.service.UserService;


@WebServlet("/adminadduser")
public class AdminAddUserServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取参数
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String role = request.getParameter("role");
		String sex = request.getParameter("sex");
		String age = request.getParameter("age");
		String createTime = request.getParameter("createTime");
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setRole(role);
		user.setSex(Integer.parseInt(sex));
		user.setAge(Integer.parseInt(age));
		user.setCreateTime(createTime);
				
		//2.调用service中的方法，实现新增
		UserService userService=new UserService();
		userService.add(user);
				
		//3.跳转到列表页(重定向？请求转发？)
		//当我们不需要向跳转的页面中传递数据的时候，就是用重定向，效率高
		response.sendRedirect(request.getContextPath()+"/adminuserlist");
	}
}
